<?php
namespace Home\Controller;
use Think\Controller;
/**
*  
*/
class PayController extends Controller{
    public function chengYiPay(){
        //类型是108，支付诚意金
        /*$password = I('post.password');
        if(!$password){$this->ajaxReturn(array('status'=>0,'message'=>'请输入密码','money'=>0));}*/
        $pay_type = I('post.pay_type');
        $user_id = get_user();
        $frize = M('frize')->where(array('user_id'=>$user_id))->find();
        if(!$frize){
            $this->ajaxReturn(array('status'=>0,'message'=>'您不需要支付诚意金','money'=>0));
        }else{
            if($frize['status'] == 1){
                if(1 == $frize['is_drop']){
                    $money = 10;
                }else{
                    $money = M('admin_setting')->where('id=1')->getField('frize_money');
                }
            }elseif($frize['status'] == 2){
                $this->ajaxReturn(array('status'=>0,'message'=>'您已被永久冻结','money'=>0));
            }else{
                $this->ajaxReturn(array('status'=>0,'message'=>'您已支付过诚意金','money'=>0));
            }
        }
        $order['type'] = 108;
        $order['user_id'] = $user_id;
        $order['add_time'] = time();
        $order['pay_type'] = $pay_type;
        $order['deal_num']='CT'.session("uid.id").time();//流水号
        $res_money = M('user')->where('id='.$user_id)->getField('money');
        if($pay_type == 0){
            //余额付款
            if($res_money<$money){
                $this->ajaxReturn(array('status'=>0,'message'=>'余额不足','money'=>0));
            }
            $order['weixin_money']=0;
            $order['yue_money']= $money;
            $order['status'] = 1;
            $order['activity_id'] = 0;
            $order['pay_type'] = 0;
            
            $res = M('order')->add($order);
            if($res){
                $moneyLog[0] = array(
                    'user_id'   => $order['user_id'],
                    'money'     => $money,
                    'add_time'  => $order['add_time'],
                    'type'      => $order['type'],
                    'pay_type'  => $order['pay_type'],
                    'activity_id'=> 0,
                    'content'   => '支付诚意金'
                );
                $moneyLog[1] = array(
                    'user_id'   => $frize['to_user_id'],
                    'money'     => $money,
                    'add_time'  => $order['add_time'],
                    'type'      => 109,
                    'pay_type'  => $order['pay_type'],
                    'activity_id'=> 0,
                    'content'   => '收到诚意金'
                );
                M('user')->where('id='.$user_id)->setDec('money',$money);
                M('user')->where('id='.$frize['to_user_id'])->setInc('money',$money);
                $log_id = M('money_log')->add($moneyLog[0]);
                M('money_log')->add($moneyLog[1]);
                M('frize')->where(array('user_id'=>$user_id))->save(array('status' => 0,'is_drop' =>0 ));
                M('order')->where('id='.$res)->save(array('log_id'=>$log_id)); 
                D('Message')->sendPayChengyiMessage($user_id);
                $this->ajaxReturn(array('status'=>1,'message'=>'支付成功'));
            }
            $this->ajaxReturn(array('status'=>0,'message'=>'支付失败'));
        }elseif($pay_type == 1){
            //微信付款
            $order['status'] = 0;
            $order['pay_type'] = 1;
            $order['weixin_money']=0.1;//$money;
            $order['yue_money']= 0;
        }else{
            //微信+余额付款
            $order['status'] = 0;
            $order['pay_type'] = 2;
            $order['yue_money']= $res_money;
            $order['weixin_money']=0.1;//$money - $res_money;
        }
        
        M('order')->add($order);
        $order['moneys']=$order['weixin_money']*100;
        Vendor('WxpayV3.WxPayPubHelper');
        $tools = new \JsApiPay();
        // $this->ajaxReturn($tools);
        //①、获取用户openid

        // // $openId = $tools->GetOpenid();
        // $openId =session("openid");
        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("平台");
        $input->SetAttach($order['user_id']);
        $input->SetOut_trade_no($order['deal_num']);
        $input->SetTotal_fee($order['weixin_money']*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("用户付款");
        $input->SetNotify_url("http://".C("INT_COM")."/index.php/Home/System/Wx_pay_ajax_oks");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid(session("uid.openid"));

        $wxorder = \WxPayApi::unifiedOrder($input);
         // $this->ajaxReturn($wxorder);
        $jsApiParameters = $tools->GetJsApiParameters($wxorder);
        // $this->ajaxReturn($jsApiParameters);
        $res["status"]=1;
        $res["code"]=100;
        $res["data"]=$jsApiParameters;
        $this->ajaxReturn($res);

    }


	public function Order_pay(){
        $password = I('post.password');
       // if(!$password){$this->ajaxReturn(array('status'=>0,'message'=>'请输入密码','money'=>0));}
        $order['user_id']=session("uid.id");
        $order['deal_num']='CT'.session("uid.id").time();//流水号
        $order['add_time']=time();
        $order['status']=0;
        //type类型
        $order['activity_id'] =I('post.id');
        $is_order = M('order')->where('activity_id='.$order['activity_id'])->find();
        if($is_order){
            $this->ajaxReturn(array('status'=>0,'message'=>'重复提交'));
        }
        $order['type']=102;
        $activi = M('activity')->where('id='.$order['activity_id'])->find();
        $user = M('user')->field("id,money,username,lat,lng")->where('id='.$order['user_id'])->find();
        //获取付款总额
        $money = intval($activi['bao_num'] * $activi['join_money'] + $activi['gift']);
        $pay_type = I('post.pay_type');
        if($pay_type == 0){
            //余额付款
            if(($user['money'] - $money) < 0){
                $this->ajaxReturn(array('status'=>0,'message'=>'余额不足'));
            }
            $order['weixin_money']=0;
            $order['yue_money']= $money;
            $order['status'] = 1;
            $order['pay_type'] = 0;
            $res = M('order')->add($order);
            if($res){
                $moneyLog = array(
                    'user_id'   => $order['user_id'],
                    'money'     => $money,
                    'add_time'  => $order['add_time'],
                    'type'      => $order['type'],
                    'pay_type'  => $order['pay_type'],
                    'activity_id'=> $order['activity_id'],
                    'content'   => '发布活动付款'
                );
                M('user')->where('id='.$order['user_id'])->setDec('money',$money);
                $log_id = M('money_log')->add($moneyLog);
                M('activity')->where('id='.$order['activity_id'])->save(array('is_pay'=>1));
                M('dealing')->where('activity_id='.$order['activity_id'])->save(array('is_pay'=>1));
                M('order')->where('id='.$res)->save(array('log_id'=>$log_id));
                $content1 = "您已成功发布活动";

                //添加发布的次数
                //查看用户是否发布次数达到5次
                $todayTimes = M('today_times')->where('user_id='.$order['user_id'])->find();
                if ($todayTimes) {
                    if ($todayTimes['add_time'] == strtotime(date("Y-m-d"))) {
                        M('today_times')->where('user_id')->setInc('fa_times',1);
                    }
                    if ($todayTimes['add_time'] < strtotime(date("Y-m-d"))) {
                        $saveArray = array('add_time'=>strtotime(date('Y-m-d')),'abolish_times'=>0,'fa_times'=>1,'bao_times'=>0);
                        M('today_times')->where('user_id='.$order['user_id'])->save($saveArray);
                    }
                }
                else{
                    //如果没有记录，则添加一条数据
                    $todayData = array(
                        'user_id'   =>$order['user_id'],
                        'fa_times'  =>0,
                        'bao_times' =>1,
                        'abolish_times' => 0,
                        'add_time'  =>strtotime(date("Y-m-d"))
                    );
                    M('today_times')->add($todayData);
                }
                $follow_where['a.fo_user_id'] = $order['user_id'];
                $followers = M('follow a')->distinct(true)
                        ->field('a.user_id,b.id,b.lat,b.lng,b.is_verify,b.sex')
                        ->join('left join zm_user b on a.user_id=b.id')
                        ->where(array('a.fo_user_id' => $order['user_id']))
                        ->select();
                $content2 = '您关注的' . $user['username'] . '发布了'. mb_substr($activi['title'],0,2,'utf-8').'活动邀约，快去报名吧';
                $ModelMess = D('Message');
                foreach ($followers as $key => $value) {
                    if(empty($value['id']) || $value['is_verify'] == 0 || 0 == $activi['type'])
                        continue;
                    if(0 != $activi['sex'] && $activi['sex'] != $value['sex'])
                        continue;
                    $distance = getdistance($value['lng'],$value['lat'],$user['lng'],$user['lat']);
                    if($distance > 50)
                        continue;
                    $ModelMess->addSysMsg($value['user_id'],$content2,$order['activity_id']);
                }
                $ModelMess->addSysMsg($order['user_id'],$content1,$order['activity_id']);
                $this->ajaxReturn(array('status'=>1,'message'=>'支付成功'));
            }
            $this->ajaxReturn(array('status'=>0,'message'=>'支付失败'));
        }elseif($pay_type == 1){
            //微信付款
            $order['status'] = 0;
            $order['pay_type'] = 1;
            $order['weixin_money']=0.01;
            $order['yue_money']= 0;
        }elseif($pay_type == 2){
            //微信+余额付款
            $order['status'] = 0;
            $order['pay_type'] = 2;
            $order['yue_money']= $user['money'];
            $order['weixin_money'] = 0.01;//$money - $user['money'];
            
        }else{
            $this->ajaxReturn(array('status'=>0,'message'=>'请选择支付方式'));
        }

        M('order')->add($order);
        //$order['moneys']=$weixin_money*100;
        Vendor('WxpayV3.WxPayPubHelper');
        $tools = new \JsApiPay();
        // $this->ajaxReturn($tools);
        //①、获取用户openid

        // // $openId = $tools->GetOpenid();
        // $openId =session("openid");
        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("平台");
        $input->SetAttach($order['user_id']);
        $input->SetOut_trade_no($order['deal_num']);
        $input->SetTotal_fee($order['weixin_money']*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("用户付款");
        $input->SetNotify_url("http://".C("INT_COM")."/index.php/Home/System/Wx_pay_ajax_oks");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid(session("uid.openid"));

        $wxorder = \WxPayApi::unifiedOrder($input);
         // $this->ajaxReturn($wxorder);
        $jsApiParameters = $tools->GetJsApiParameters($wxorder);
        // $this->ajaxReturn($jsApiParameters);
        $followers = M('follow a')->distinct(true)
            ->field('a.user_id,b.id,b.lat,b.lng,b.is_verify')
            ->join('left join zm_user b on a.user_id=b.id')
            ->where(array('a.fo_user_id' => $order['user_id']))
            ->select();


        $ModelMess = D('Message');
        $content2 = '您关注的' . $user['username'] . '发布了'. mb_substr($activi['title'],0,2,'utf-8').'活动邀约，快去报名吧';
        $userArr = array();
        foreach ($followers as $key => $value) {
            if(empty($value['id']) || $value['is_verify'] == 0 || 0 == $activi['type'])
                continue;
            if(0 != $activi['sex'] && $activi['sex'] != $value['sex'])
                continue;
            $distance = getdistance($value['lng'],$value['lat'],$user['lng'],$user['lat']);
            if($distance > 50)
                continue;
            $userArr[] = $value['user_id'];
        }
        $content1 = "您已成功发布了". mb_substr($activi['title'],0,2,'utf-8')."活动";
        $ModelMess->addSysMsg($order['user_id'],$content1,$order['activity_id']);

        $ModelMess->addSysMsges($userArr,$content2,$order['activity_id']);
        $res["status"]=1;
        $res["code"]=100;
        $res["data"]=$jsApiParameters;
        $this->ajaxReturn($res);
	}


    public function moneyPayNeed(){
        $id = I('post.id');
        $user_id = get_user();
        $yue_money = M('user')->where('id='.$user_id)->getField('money');
        //查找影虎
        if(!$user_id){
            $this->ajaxReturn(array('status'=>0,'message'=>'请先登录','money'=>0,'yue_money'=>0));
        }
        if($id == 0){
            //状态1表示已经被冻结
            $frize = M('frize')->where(array('user_id'=>$user_id))->find();
            if(!$frize){
                $this->ajaxReturn(array('status'=>0,'message'=>'您不需要支付诚意金','money'=>0,'yue_money'=>$yue_money));
            }else{
                if($frize['status'] == 1){
                    if(1 == $frize['is_drop']){
                        $frize_money = 10;
                    }else{
                        $frize_money = M('admin_setting')->where('id=1')->getField('frize_money');
                    }
                    
                    $money = $frize_money;
                    $this->ajaxReturn(array('status'=>1,'message'=>'需要支付诚意金','money'=>$money,'yue_money'=>$yue_money));
                }elseif($frize['status'] == 2){
                	$this->ajaxReturn(array('status'=>0,'message'=>'您的账户已被永久冻结，无法支付诚意金','money'=>$money,'yue_money'=>$yue_money));
                }
                else{
                    $this->ajaxReturn(array('status'=>0,'message'=>'您已支付过诚意金','money'=>0,'yue_money'=>$yue_money));
                }
            }
        }else{
            //$id 不为0.表示支付发布活动的金额
            $activi = M('activity')->where('id='.$id)->find();
            if(!$activi){
                //如果不存在活动，则返回0
                 $this->ajaxReturn(array('status'=>0,'message'=>'活动不存在','money'=>0,'yue_money'=>$yue_money));
            }
            if($activi['is_pay'] == 1){
                $this->ajaxReturn(array('status'=>0,'message'=>'活动已经支付过','money'=>0,'yue_money'=>$yue_money));
            }
            $money = $activi['bao_num'] * $activi['join_money'] + $activi['gift'];
            $this->ajaxReturn(array('status'=>1,'message'=>'请求成功','money'=>$money,'yue_money'=>$yue_money));
        }
    }


}